// ChatDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VaxVoIPSample.h"
#include "ChatDlg.h"
#include "afxdialogex.h"
#include "VaxSIPUserAgent.h"

#define CHAT_TYPING_TIMER   WM_USER + 1

IMPLEMENT_DYNAMIC(CChatDlg, CPropertyPage)

CChatDlg::CChatDlg(CVaxSIPUserAgent* pVaxSIPUserAgent): CPropertyPage(CChatDlg::IDD)
{
	m_psp.dwFlags &= ~PSP_HASHELP; 
	m_pVaxSIPUserAgent = pVaxSIPUserAgent;

	m_objFont.CreateFont(14, 0, 0, 0, FW_BOLD, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Verdana"); 

     m_sLblChatStatus = "";
	 m_sEditChatContact = "";
	 m_sEditChatUserName = "";
     m_sEditSendChatMessage = "";
}

CChatDlg::~CChatDlg()
{
}

void CChatDlg::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CMBO_STATUS, m_ctrlComboMyChatStatus);
	DDX_Control(pDX, IDC_LIST_CHAT_STATUSLOG , m_ctrlListChatLog);
	DDX_Control(pDX, IDC_LIST_CHATCONTACTS, m_ctrlCListChatContact);
	DDX_Control(pDX, IDC_LIST_CHATMESSAGES, m_ctrlListChatMessage);
	DDX_Text(pDX, IDC_LBLCHATSTUTUS, m_sLblChatStatus);
	DDX_Text(pDX, IDC_EDIT_CHAT_CONTACT_NUMBER, m_sEditChatContact);
	DDX_Text(pDX, IDC_EDIT_CHAT_USER_NAME, m_sEditChatUserName);
	DDX_Text(pDX, IDC_EDIT_CHAT_MESSAGE, m_sEditSendChatMessage);

	GetDlgItem(IDC_GROUP_ADDCHATCONTACT)->SetFont(&m_objFont);
	GetDlgItem(IDC_GROUP_CHAT_MESSAGES)->SetFont(&m_objFont);
	GetDlgItem(IDC_GROUP_CHAT_STATUSLOG)->SetFont(&m_objFont);

}

BEGIN_MESSAGE_MAP(CChatDlg, CPropertyPage)
	ON_BN_CLICKED(IDC_BTN_ADD_NUMBER, &CChatDlg::OnBtnClickedAddContact)
	ON_BN_CLICKED(IDC_BTN_REMOVE_NUM, &CChatDlg::OnBtnClickedRemoveContact)
	ON_BN_CLICKED(IDC_BTN_CLEAR_MESSAGE, &CChatDlg::OnBtnClickedClearChat)
	ON_BN_CLICKED(IDC_BTN_SEND_MESSAGE, &CChatDlg::OnBtnClickedSendMessage)
	ON_BN_CLICKED(IDC_BTN_CHAT_CLEAR, &CChatDlg::OnBtnClickedClearLog)
	ON_WM_TIMER()
	ON_EN_CHANGE(IDC_EDIT_CHAT_MESSAGE, &CChatDlg::OnChangeEditMessage)
	ON_CBN_SELCHANGE(IDC_CMBO_STATUS, &CChatDlg::ComboMyChatStatus_SelectedIndexChanged)

END_MESSAGE_MAP()

void CChatDlg::OnVaxInitialize()
{
	UpdateStatusCombo();
}

void CChatDlg::OnVaxUnInitialize()
{
	m_ctrlCListChatContact.DeleteAllItems();
	m_ctrlComboMyChatStatus.ResetContent();
}

void CChatDlg::UpdateStatusCombo()
{
	m_ctrlComboMyChatStatus.InsertString(0, "Online");
	m_ctrlComboMyChatStatus.InsertString(1, "Offline");
	m_ctrlComboMyChatStatus.InsertString(2, "Away");
	m_ctrlComboMyChatStatus.InsertString(3, "On the phone");
	m_ctrlComboMyChatStatus.InsertString(4, "Busy");

	m_ctrlComboMyChatStatus.SetCurSel(1);
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////

void CChatDlg::OnChatContactStatus(CString sUserName, int nStatusId)
{
	m_ctrlCListChatContact.SetItemText(m_ctrlCListChatContact.FindItemIndex(sUserName), 1, GetStatusText(nStatusId));
}

void CChatDlg::OnChatSendMsgTextSuccess(LPCTSTR sUserName, LPCTSTR sMsgText, int nUserValue32bit)
{
    AddToChatLog("Send Message Success.");
}

void CChatDlg::OnChatSendMsgTextFail(LPCTSTR sUserName, int nStatusCode, LPCTSTR sReasonPhrase, LPCTSTR sMsgText, int nUserValue32bit)
{
    AddToChatLog("Send Message Fail.");
}

void CChatDlg::OnChatSendMsgTypingSuccess(LPCTSTR sUserName, int nUserValue32bit)
{
	
}
        
void CChatDlg::OnChatSendMsgTypingFail(LPCTSTR sUserName, int nStatusCode, LPCTSTR sReasonPhrase, int nUserValue32bit)
{
	
}

void CChatDlg::OnChatRecvMsgText(CString sUserName, CString sMsgText)
{
	int nstrStart = sMsgText.Find(">");

    if(nstrStart == -1)
    {
		UpdateChatMessageBox(sUserName + ": " + sMsgText);
        return;
    }
        
    nstrStart = nstrStart + 1;
	int nstrEnd = sMsgText.Find("<", nstrStart);

	UpdateChatMessageBox(sUserName + ": " + sMsgText.Mid(nstrStart, nstrEnd - nstrStart));
}

void CChatDlg::OnChatRecvMsgTypingStart(CString sUserName)
{
    m_bTyping = TRUE;

	GetDlgItem(IDC_LBLCHATSTUTUS)->SetWindowTextA(sUserName + " is typing message");

	SetTimer(CHAT_TYPING_TIMER, 1000, NULL); 

    IsTyping();
}

void CChatDlg::OnChatRecvMsgTypingStop()
{
    m_sLblChatStatus = "";
	UpdateData(FALSE);
}

void CChatDlg::AddToChatLog(LPCTSTR sChatLog)
{
	m_ctrlListChatLog.AddString(sChatLog);
}

CString CChatDlg::GetStatusText(int nStatusId)
{
    CString sStatus = "Offline";

    switch (nStatusId)
    {
        case 0:
            sStatus = "Online";
            break;

        case 1:
			sStatus = "Offline";
            break;

        case 2:
            sStatus = "Away";
            break;

        case 3:
            sStatus = "On the phone";
            break;

        case 4:
            sStatus = "Busy";
            break;
    }

    return sStatus;
}

void CChatDlg::IsTyping()
{
    if(!m_bTyping)
    {
        OnChatRecvMsgTypingStop();

        KillTimer(CHAT_TYPING_TIMER);
    }
    else
        m_bTyping = false;
}      

void CChatDlg::OnBtnClickedAddContact()
{
	UpdateData(TRUE);
	BOOL bResult = m_pVaxSIPUserAgent->ChatAddContact(m_sEditChatContact);

    if (!bResult) 
	{
	    return;
	}

	m_ctrlCListChatContact.InsertItemRow(m_ctrlCListChatContact.GetItemCount(), m_sEditChatContact);

    m_sEditChatContact = "";
	
	UpdateData(FALSE);
}

void CChatDlg::OnBtnClickedRemoveContact()
{
	int nIndex = m_ctrlCListChatContact.GetCurSelItem();
	
	BOOL bResult = m_pVaxSIPUserAgent->ChatRemoveContact(m_ctrlCListChatContact.GetItemText(nIndex, 0));
	
	if(!bResult)
       return;

	m_ctrlCListChatContact.DeleteItem(nIndex);
}

void CChatDlg::OnBtnClickedClearChat()
{
	m_ctrlListChatMessage.ResetContent();
}

void CChatDlg::OnBtnClickedSendMessage()
{
	UpdateData(TRUE);
	if (m_sEditChatUserName.GetLength() == 0)
        {
            MessageBox("Please provide the User name");
            return;
        }

    BOOL bResult = m_pVaxSIPUserAgent->ChatSendMessageText(m_sEditChatUserName, m_sEditSendChatMessage, 101, 0);
    if(!bResult)
	{
	    return;
	}

	UpdateChatMessageBox("Me:" + m_sEditSendChatMessage);
    m_sEditSendChatMessage = "";
	UpdateData(FALSE);
}

void CChatDlg::OnBtnClickedClearLog()
{
	m_ctrlListChatLog.ResetContent();
}

void CChatDlg::OnTimer(UINT_PTR nIDEvent)
{
	IsTyping();

	CPropertyPage::OnTimer(nIDEvent);
}

void CChatDlg::UpdateChatMessageBox(LPCTSTR sChatMessage)
{
	m_ctrlListChatMessage.AddString(sChatMessage);
}

void CChatDlg::OnChangeEditMessage()
{
	UpdateData(TRUE);
	BOOL bResult = m_pVaxSIPUserAgent->ChatSendMessageTyping(m_sEditChatUserName, 1);
}

void CChatDlg::ComboMyChatStatus_SelectedIndexChanged()
{
   if(m_ctrlComboMyChatStatus.GetCurSel() == -1) return;

   BOOL bResult = m_pVaxSIPUserAgent->ChatSetMyStatus(m_ctrlComboMyChatStatus.GetCurSel());

   if(!bResult) return;  
}

BOOL CChatDlg::OnInitDialog()
{
	CPropertyPage::OnInitDialog();

	m_ctrlCListChatContact.InsertColumn(0, "Contact", 0, 76);
	m_ctrlCListChatContact.InsertColumn(1, "Status", 0, 168);

	m_ctrlCListChatContact.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_AUTOSIZECOLUMNS);

	return TRUE; 
}

BOOL CChatDlg::OnApply()
{
	return FALSE;
	//return CPropertyPage::OnApply();
}



void CChatDlg::OnLbnSelchangeListchatmsg()
{
	// TODO: Add your control notification handler code here
}
